package com.Food;

import java.util.ArrayList;
import java.util.Scanner;

/*
    1.上架菜品
    2.下架菜品
    3.展示全部菜品
    4.根据价格查询菜品
    5.根据名字查询菜品
    6.根据编号查询菜品
    7.修改菜品
    8.退出系统
*/
public class DishManager {
    //首先定义一个集合用来存放菜品数据
    private ArrayList<Dish> dishes = new ArrayList<>();

    /**
     * 调用所有功能的方法
     */
    public void showMena() {
        System.out.println("=====欢迎来到青春食堂=====");
        while (true) {
            System.out.println("请输入操作命令：");
            System.out.println("1.上架菜品");
            System.out.println("2.下架菜品");
            System.out.println("3.展示全部菜品");
            System.out.println("4.根据价格查询菜品");
            System.out.println("5.根据名字查询菜品");
            System.out.println("6.根据编号查询菜品");
            System.out.println("7.修改菜品");
            System.out.println("8.退出系统");
            Scanner sc = new Scanner(System.in);
            //键盘接收命令
            int commant = sc.nextInt();
            switch (commant) {
                case 1 -> {
                    //1.上架菜品
                    addDish();
                }
                case 2 -> {
                    //2.下架菜品
                    System.out.println("请输入你要下架的菜品的编号：");
                    String id = sc.next();
                    delDish(id);
                }
                case 3 -> {
                    //3.展示全部菜品
                    showAllDishInfo();
                }
                case 4 -> {
                    //4.根据价格查询菜品
                    System.out.println("请输入菜品价格最小值：");
                    double minprice = sc.nextDouble();
                    System.out.println("请输入菜品价格最大值：");
                    double maxprice = sc.nextDouble();
                    searchDishByPrice(minprice, maxprice);
                }
                case 5 -> {
                    //5.根据名字查询菜品
                    System.out.println("请输入你要查询菜品的名字：");
                    String name = sc.next();
                    searchDishByName(name);
                }
                case 6 -> {
                    //6.根据编号查询菜品
                    System.out.println("请输入你要查询菜品的id:");
                    String id = sc.next();
                    Dish dishById = getDishById(id);
                    if (dishById != null) {
                        showDishsingleInfo(dishById);
                    } else {
                        System.out.println("你输入的id不存在！！！");
                    }

                }
                case 7 -> {
                    //7.修改菜品
                    System.out.println("请输入你要修改菜品的id:");
                    String id = sc.next();
                    changeDishInfo(id);
                }
                case 8 -> {
                    System.out.println("欢迎来到青春食堂，期待你的下次光临！！！");
                    return;
                }
            }
        }

    }


    /**
     * 上架菜品
     */
    public void addDish() {
        Scanner sc = new Scanner(System.in);
        System.out.println("===上架菜品功能===");
        System.out.println("请输入添加的菜品编号：");
        String id = sc.next();
        System.out.println("请输入添加的菜品名称：");
        String name = sc.next();
        System.out.println("请输入添加的菜品价格：");
        double price = sc.nextDouble();
        System.out.println("请输入添加的菜品优惠价：");
        double discountPrice = sc.nextDouble();
        System.out.println("请输入添加的菜品标签(素菜/荤菜)：");
        String lable = sc.next();
        System.out.println("请输入添加的菜品口味：");
        String taste = sc.next();
        System.out.println("请输入添加的菜品食材,请以‘,’号隔开：");
        String food = sc.next();
        String[] foods = food.split(",");
        //把数据收据到Dish对象里面
        Dish dishs = new Dish(id, name, price, discountPrice, lable, taste, foods);
        //添加到集合中
        dishes.add(dishs);
        System.out.println(dishs.getName()+"菜品已上架！");
    }

    /**
     * 2.下架菜品
     */
    public void delDish(String id) {
        Dish dishById = getDishById(id);
        if (dishById != null) {
            dishes.remove(dishById);
            System.out.println(id + "号菜品下架成功~~~");
        }
    }

    /**
     * 3.展示全部菜品
     */
    private void showAllDishInfo() {
        if (dishes.size() == 0) {
            System.out.println("菜品一个都没有，看个锤子啊~~~");
            return;
        }
        for (int i = 0; i < dishes.size(); i++) {
            Dish dish = dishes.get(i);
            System.out.println("菜品编号为：" + dish.getId());
            System.out.println("菜品的名称：" + dish.getName());
            System.out.println("菜品的价格：" + dish.getPrice());
            System.out.println("菜品的优惠价：" + dish.getDiscountPrice());
            System.out.println("菜品的标签：" + dish.getLabel());
            System.out.println("菜品的口味：" + dish.getTaste());
            String[] foodlist = dish.getFoods();
            System.out.print("菜品的食材：");
            for (int j = 0; j < foodlist.length; j++) {
                System.out.print(foodlist[j] + ",");
            }
            System.out.println();
        }
    }

    /**
     * 展示某个id的所有信息
     *
     * @param dishById 展示菜品的id
     */
    private void showDishsingleInfo(Dish dishById) {
        System.out.println("菜品编号为：" + dishById.getId());
        System.out.println("菜品的名称：" + dishById.getName());
        System.out.println("菜品的价格：" + dishById.getPrice());
        System.out.println("菜品的优惠价：" + dishById.getDiscountPrice());
        System.out.println("菜品的标签：" + dishById.getLabel());
        System.out.println("菜品的口味：" + dishById.getTaste());
        String[] foodlist = dishById.getFoods();
        System.out.print("菜品的食材：");
        for (int j = 0; j < foodlist.length; j++) {
            System.out.print(foodlist[j] + ",");
        }
        System.out.println();
    }

    /**
     * 4.根据价格查询菜品
     *
     * @param minprice 价格最小范围
     * @param maxprice 价格最大范围
     */
    private void searchDishByPrice(double minprice, double maxprice) {
        if (dishes.size() == 0) {
            System.out.println("菜单一个都没有，查你妹啊~~~");
            return;
        }
        ArrayList<Dish> newdishes = new ArrayList<>();
        for (int i = 0; i < dishes.size(); i++) {
            Dish dishs = dishes.get(i);
            if (dishs.getPrice() >= minprice && dishs.getPrice() <= maxprice) {
                newdishes.add(dishs);
            }
        }
        for (int i = 0; i < newdishes.size(); i++) {
            Dish dish = newdishes.get(i);
            System.out.println("菜品编号为：" + dish.getId());
            System.out.println("菜品的名称：" + dish.getName());
            System.out.println("菜品的价格：" + dish.getPrice());
            System.out.println("菜品的优惠价：" + dish.getDiscountPrice());
            System.out.println("菜品的标签：" + dish.getLabel());
            System.out.println("菜品的口味：" + dish.getTaste());
            String[] foodlist = dish.getFoods();
            System.out.print("菜品的食材：");
            for (int j = 0; j < foodlist.length; j++) {
                System.out.print(foodlist[j] + ",");
            }
            System.out.println();
        }
    }

    /**
     * 5.根据名字查询菜品
     *
     * @param name 查询菜品的名字
     */
    private void searchDishByName(String name) {
        if (dishes.size() == 0) {
            System.out.println("菜单一个都没有，查你妹啊~~~");
            return;
        }
        ArrayList<Dish> newdishes = new ArrayList<>();
        for (int i = 0; i < dishes.size(); i++) {
            Dish dishs = dishes.get(i);
            if (dishs.getName().contains(name)) {
                newdishes.add(dishs);
            }
        }
        for (int i = 0; i < newdishes.size(); i++) {
            Dish dish = newdishes.get(i);
            System.out.println("菜品编号为：" + dish.getId());
            System.out.println("菜品的名称：" + dish.getName());
            System.out.println("菜品的价格：" + dish.getPrice());
            System.out.println("菜品的优惠价：" + dish.getDiscountPrice());
            System.out.println("菜品的标签：" + dish.getLabel());
            System.out.println("菜品的口味：" + dish.getTaste());
            String[] foodlist = dish.getFoods();
            System.out.print("菜品的食材：");
            for (int j = 0; j < foodlist.length; j++) {
                System.out.print(foodlist[j] + ",");
            }
            System.out.println();
        }
    }

    /**
     * 7.修改菜品
     *
     * @param id 修改菜品的id
     */
    private void changeDishInfo(String id) {
        if (dishes.size() == 0) {
            System.out.println("菜单一个都没有，改你妹啊~~~");
            return;
        }
        for (int i = 0; i < dishes.size(); i++) {
            Dish dishs = dishes.get(i);
            if (dishs.getId().equals(id)) {
                System.out.println("请输入你要改哪个信息：");
                System.out.println("1、菜品编号");
                System.out.println("2、菜品的名称");
                System.out.println("3、菜品的价格");
                System.out.println("4、菜品的优惠价");
                System.out.println("5、菜品的标签");
                System.out.println("6、菜品的口味");
                System.out.println("7、菜品的食材");
                Scanner sc = new Scanner(System.in);
                int communt = sc.nextInt();
                switch (communt) {
                    case 1 -> {
                        System.out.println("请输入你要更改的菜品编号：");
                        String nextId = sc.next();
                        dishs.setId(nextId);
                        System.out.println("修改成功！");
                    }
                    case 2 -> {
                        System.out.println("请输入你要更改的菜品名字：");
                        String nextName = sc.next();
                        dishs.setName(nextName);
                        System.out.println("修改成功！");
                    }
                    case 3 -> {
                        System.out.println("请输入你要更改的菜品价格：");
                        double nextPrice = sc.nextDouble();
                        dishs.setPrice(nextPrice);
                        System.out.println("修改成功！");
                    }
                    case 4 -> {
                        System.out.println("请输入你要更改的菜品优惠价格：");
                        double nextDiscountPrice = sc.nextDouble();
                        dishs.setDiscountPrice(nextDiscountPrice);
                        System.out.println("修改成功！");
                    }
                    case 5 -> {
                        System.out.println("请输入你要更改的菜品标签：");
                        String nextLabel = sc.next();
                        dishs.setLabel(nextLabel);
                        System.out.println("修改成功！");
                    }
                    case 6 -> {
                        System.out.println("请输入你要更改的菜品口味：");
                        String nextTaste = sc.next();
                        dishs.setTaste(nextTaste);
                        System.out.println("修改成功！");
                    }
                    case 7 -> {
                        System.out.println("请输入添加的菜品食材,请以‘,’号隔开：");
                        String food = sc.next();
                        String[] nextFoods = food.split(",");
                        dishs.setFoods(nextFoods);
                        System.out.println("修改成功！");
                    }

                }
            }
        }

    }

    /**
     * 根据id查找对应的菜品
     *
     * @param id 传入要找的菜品的id
     */
    public Dish getDishById(String id) {
        if (dishes.size() == 0) {
            System.out.println("菜单一个都没有，查你妹啊~~~");
            return null;
        }
        for (int i = 0; i < dishes.size(); i++) {
            Dish dishs = dishes.get(i);
            if (dishs.getId().equals(id)) {
                return dishs;
            }
        }
        return null;
    }
}
